/**
 * @author of2198
 * @date 2017/4/27.
 * @description
 */

import { QL } from "iflux2";
import { fromJS } from "immutable";
import { getSystemCode } from "../common/const-value";

const systemCode = getSystemCode();

export const pageTypeQL = QL("pageTypeQL", [
  "pageTypeBtns",
  pageTypeBtns => {
    //把pageType和名称对应起来  方便取用
    let pageTypeInfo = {};
    pageTypeBtns.forEach(item => {
      if (item.typeList && item.typeList.length > 0) {
        item.typeList.forEach(type => {
          pageTypeInfo[type] = item.name;
        });
      }
    });
    return pageTypeInfo;
  }
]);

const DomainInfo = {
  d2c: {
    pc: {
      type: "c",
      platform: "pc"
    },
    weixin: {
      type: "w",
      platform: "weixin"
    }
  },
  d2p: {
    pc: {
      type: "b",
      platform: "pc"
    },
    weixin: {
      type: "x",
      platform: "weixin"
    },
    app: {
      type: "a",
      platform: "app"
    }
  }
};
export const domainListQL = QL("domainListQL", [
  "domainList",
  domainList => {
    let domainInfo = {};
    fromJS(DomainInfo[systemCode]).forEach((item, platform) => {
      let result = domainList.filter(domain => {
        return (
          domain.get("dtype") === item.get("type") &&
          domain.get("domainDefault") == "Y"
        );
      });
      let domainUrl = result.getIn([0, "domainUrl"]);
      //微信自定义域名加上A编号
      if (
        systemCode === "D2C" &&
        result.getIn([0, "dtype"]) === "w" &&
        result.getIn([0, "domainName"]) === "selfdom"
      ) {
        domainUrl = domainUrl + "/" + result.getIn([0, "adminId"]);
      }
      domainInfo[platform] = domainUrl;
    });
    return domainInfo;
  }
]);
